<?php

class jishuAction extends SaleAction
{
    public function index()
    {
        $model = new Model();
        $members = $model->table('ys_project p,ys_employee e,ys_customer c')
            ->where(' p.employee=e.id and p.custid=c.id and   p.isdelete=0')
            ->field('p.*,e.name as employeename,c.company as companyname,c.telephone ,c.contact, c.address as address,e.department as depart  ')
            ->order('p.id desc')
            ->select();

        /* echo $model->getLastSql();
           dump($members);exit();  */

        //   echo $model->getLastSql();exit();
        //   dump($projectdata);


        $department = $this->CacheArray("department");
        $projecttype = $this->CacheArray("projecttype");
        foreach ($members as $k => &$v) {
            $v["departname"] = $department[$v["depart"]]["name"];
            $v["projecttype"] = $projecttype[$v["type"]]["name"];

        }

        $this->list = $members;
        $this->display();


        /*     $model = new Model();
             $employee = I('session.userid', 0);
             $wherecondition = "p.custid=c.id and  p.employee=" . $employee . " and p.type=t.id and e.department=d.id and p.isdelete=0 and  p.status=1 and e.id=" . $employee;
             $projectdata = $model->table('ys_project p ,ys_customer  c,ys_projecttype t,ys_employee e,ys_department d')
                 ->where($wherecondition)
                 ->field('p.id as pid,c.*,t.name as tname,e.name as ename,d.name as dname')
                 ->order('p.id')
                 ->select();

             //echo $model->getLastSql();exit();
             $this->list = $projectdata;
             $this->display();*/
    }

    public function back()
    {
        $this->display();
    }

    //从技术部上退回单子
    public function tuihui()
    {

        $projectid = I("post.id", 0);
        $statusnote = I("post.statusnote", "");
        $data["id"] = $projectid;
        $data["status"] = -1;
        $data["statusnote"] = $statusnote;
        $data["audittime"] = date("Y-m-d H:i:s");

        $project = M("project");
        $url = U("/jishu/index");
        $this->assign("jumpUrl", $url);

        if ($project->save($data))
            $this->success("退回成功");
        else
            $this->error("退回失败");
        exit();
    }

    public function gongzuoliang()
    {
        $task = M("tasktongji");
        $condition["creator"] = I('session.userid', 0);
        $data = $task->where($condition)->order("id desc")->select();

        $tasktype = $this->CacheArray("tasktype");
        $employee = $this->CacheArray("employee");
        foreach ($data as $k => &$v) {
            $v["typename"] = $tasktype[$v["type"]]["name"];
            $v["employeename"] = $employee[$v["taker"]]["name"];
        }


        $this->tasklist = $data;
        $this->display();
    }

    public function keyword()
    {

        $projectid=I("param.projectid");
        $project=M("project");
        $pro_info=$project->find($projectid);


        if($pro_info["status"]>=KEY_SAVED)
        {
            redirect(U("yuming",array("projectid"=>$projectid)),2,"关键词已经保存,转向域名分配");
        }
        $keywords=$pro_info["keyword"];
        $key_arr=explode("\r\n",$keywords);
        if(!$key_arr)
            $key_arr=explode(" ",$keywords);


        $this->list=$key_arr;
        $this->projectid=$projectid;
        $this->display();
    }

    public function  renwu()
    {
        Load('extend');
        $site = M('site');
        $newtypdata = $this->CacheArray("tasktype");
        $name = I('post.sousuo');
        if (IS_POST || !empty($name))
        {
            $where['name'] = array('like', "%{$name}%");
            $data = $site->where($where)->order('id desc')->select();
        } else
        {

            $data = $site->order('id desc')->select();
        }
        $task = M("task");
        $employee = $this->CacheArray("employee");
        $model = new Model();
        foreach ($data as $k1 => &$v1)
        {
            $taskcon["site"] = $v1["id"];
            $taskdata = $task->where($taskcon)->select();
            foreach ($taskdata as $k => &$v)
            {
                $v["type"] = $newtypdata[$v["type"]]["name"];
                $v["employeename"] = $employee[$v["taker"]]["name"];
            }
            $v1["task"] = $taskdata;
            $sql = "select p.id as pid, e.* from ys_project p,ys_employee e where p.employee=e.id and p.id=" . $v1["projectid"];

            $empdata = $model->query($sql);
            $v1["employeename"] = $empdata[0]["name"];
            $v1["pid"] = $empdata[0]["pid"];
        }

        $this->list = $data;
        $this->display();
    }

    public function shenhe()
    {
        $id = I('get.id');
        $this->id = $id;
        $projectdata=$this->getProjectInfo($id);
        $this->assign('shen', $projectdata);
        $this->display();
    }



    public function shenhewan()
    {

        $model = new Model();
        $members = $model->table('ys_project p,ys_employee e,ys_customer c')
            ->where(' p.employee=e.id and p.custid=c.id and   p.`status`=2 and p.isdelete=0')
            ->field('p.*,e.name as employeename,c.company as companyname,c.telephone ,c.contact, c.address as address,e.department as depart  ')
            ->order('p.id')
            ->select();

        /*echo $model->getLastSql();
         dump($members);exit(); */

        //   echo $model->getLastSql();exit();
        //   dump($projectdata);


        $department = $this->CacheArray("department");
        $projecttype = $this->CacheArray("projecttype");
        foreach ($members as $k => &$v) {
            $v["departname"] = $department[$v["depart"]]["name"];
            $v["projecttype"] = $projecttype[$v["type"]]["name"];

        }

        $this->list = $members;
        $this->display();


        /*   $model = new Model();
           $employee = I('session.userid', 0);
           $name = I('post.sousuo');
           $wherecondition = "p.custid=c.id and  p.employee=" . $employee . " and p.type=t.id and e.department=d.id and p.isdelete=0 and  p.status=2 and e.id=" . $employee;
           if (IS_POST || !empty($name)) {
               $wherecondition = $wherecondition . " and c.company like '%{$name}%'";
           }

           $projectdata = $model->table('ys_project p ,ys_customer  c,ys_projecttype t,ys_employee e,ys_department d')
               ->where($wherecondition)
               ->field('p.id as pid,c.*,t.name as tname,e.name as ename,d.name as dname')
               ->order('p.id')
               ->select();

           //echo $model->getLastSql();exit();
           $this->list = $projectdata;
           $this->display();*/
    }

    public function shenhewanxx()
    {
        $this->display();
    }

    public function savekeyword()
    {
        $keyword=I("post.keyword");
        $price=I("post.price");
        $projectid=I("post.projectid");
        $key_mod=D("keyword");
        for($i=0;$i<count($keyword);$i++)
        {
            $data["name"] = $keyword[$i];
            $data["price"] =( $price[$i] == "" ? 0 : $price[$i]);
            $data["projectid"]=$projectid;
            $key_mod->add($data);
            unset($data);
        }
        M("project")->where("id=".$projectid)->setField("status",KEY_SAVED);//设置工程状态
        $this->assign("jumpUrl",U("yuming",array("projectid"=>$projectid)));
        $this->success("工程关键词保存成功,请为关键词分配网站");
    }

    public function yuming()
    {
        $projectid = I('param.projectid');
        $project = M('project');
        $projectdata = $project->find($projectid);
        if($projectdata["status"] == TASK_ASSIGNED)
        {
            redirect(U("task/mytask"),0,"任务已经分配完成");
        }
        //------------------------------------------任务分配完,跳转到任务列表
        $keyword = $projectdata["keyword"];
        $custid = $projectdata["custid"];
        $box = explode("\n", $keyword);
        $box = array_filter($box);

        $keyword = M("keyword");
        $condition["projectid"] = $projectid;
        $condition["siteid"] =0;
        $assignedkey = $keyword->where($condition)->select();
        $this->li = $assignedkey;


        $this->pid = $projectid;

        $this->assign('pro', $projectdata);

        $customer = M("customer");
        $customerdata = $customer->find($custid);
        $companyname = $customerdata["company"];
        $this->companyname = $companyname;
        $this->display();
    }

    public function modify()
    {
        $this->display();
    }

    public function addsite()
    {

        $word =  I('post.keyword') ;
        $keywordname = implode(',', $word);
        if(!$word) $this->error("没有选择关键词");

        $site = D('site');
        $data = $site->create();
        if (!$data) $this->error($site->getError());

        $domain = trim($data["domain"]);
        $domain = substr($domain, strpos($domain, "www"));
        if (substr($domain, -1) == '/') $domain = substr($domain, 0, -1);


        $data["domain"] = $domain;
        $data["createtime"] = date("Y-m-d H:i:s");
        $data["keyword"] = $keywordname;
        if (!isset($data["beian"])) $data["beian"] = 0;
        $projectid = $data["projectid"];
        $siteid = $site->add($data);
        if (!$siteid) $this->error("网站保存失败");

        $project = M("project");

        $projectinfo = $project->find($projectid);
        $custid = $projectinfo["custid"];


        //保存关键字
        $keyword = M('keyword');
        $data["siteid"] = $siteid;
        $data["customer"] = $custid;
        $data["projectid"] = $projectid;
        $keyword->where("id in($keywordname)")->save($data);
        unset($data);

        //判断是否需要从新分配
        $condition["projectid"]=$projectid;
        $condition["siteid"]=0;
        $awords=$keyword->where($condition)->count();//已分配关键字列表

        $jumpurl = U("jishu/renwu");
        $message = "创建成功";

        if ( $awords>0) {
            $message .= "请继续分配";
            $jumpurl = U("jishu/yuming", array('projectid' => $projectid));
        } else {
            $data["id"] = $projectid;
            $data["status"] = TASK_ASSIGNED;
            $project->save($data); //更新工程状态
        }
        $this->assign("jumpUrl", $jumpurl);
        $this->success($message);


    }

    public function sitedel()
    {
        $site = M('site');
        $id = $_GET['id'];
        $res = $site->delete($id);
        $task = M('task');
        $where['site'] = $id;
        $count = $task->where($where)->delete();
//	 echo $task->getLastSql();exit();
        if ($res > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function taskdel()
    {
        $task = M('task');
        $id = $_GET['id'];
        $res = $task->delete($id);
        if ($res > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function create()
    {
        $siteid = I('get.id');
        if (empty($siteid)) $this->error("参数传递错误");
        $tasktype = M('tasktype');
        $where['parentid'] = 0;
        $where['level'] = 1;
        $where['id'] = array("exp", "not in (SELECT type from ys_task where site=$siteid )");

        $this->defaulttime = date("Y-m-d H:i:s", strtotime("+5 days"));
        $this->list = $tasktype->where($where)->select();

        $model = new Model();
        $projectdata = $model->table('ys_site s ,ys_project p')
            ->where(" s.id=" . $siteid . " and p.id=s.projectid ")
            ->field('s.*,p.content as pcon')
            ->find();
// echo $model->getLastSql();exit();
        $this->assign('zi', $projectdata);
        $this->display();
    }

    public function addtask()
    {
        $task = D('task');
        $data = $task->create();
        if (!$data) {
            $this->error($data->getError());
        } else {
            $data["status"] = 0;
            if ($data['orders'] > 1) { //如果不是立即生效,那么创建时间为空
                $data["status"] = NOT_AVL;
            } else {
                if ($data["interval"] < 0) $this->error("工期传递出错");
                $data["createtime"] = date("Y-m-d H:i:s");
                $data["deadline"] = date("Y-m-d H:i:s", strtotime("+" . $data["interval"] . " days"));
            }

            $data['nextid'] = $data['order'] + 1;
            $data['preid'] = $data['order'] - 1;

            $data["creator"] = I('session.userid', 0);


            $data["level"] =1;//规定好级别与父级别
            $data["parentid"] = 0;
            $taskid = $task->add($data);
            $newdata["id"]=$taskid;
            $newdata["path"]="0,".$taskid.",";
            $task->save($newdata);
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("jishu/create", array("id" => $data["site"] )  ));
            $this->success("创建成功");
        }

    }

    public function pingfen()
    {
        $task = M('task');
        $data = array();
        $data['id'] = I('post.fenid');
        $data['score'] = I('post.fen');
        if ($data['score'] > 100) {
            $data['score'] = 100;
        } else if ($data['score'] < 0) {
            $data['score'] = 0;
        }
        $task->save($data);
        $this->redirect('jishu/renwu');

    }

}